$(function () {
    // videojs("#myVideo").ready(function () {
    //     var myPlayer = this;
    //     // myPlayer.play();
    // });

    var music = document.getElementById('myAudio')
        , $audioSlider = $('#audioSlider')
        , $audioPlayer = $('#audioPlayer')
        , interval = null
        , $start = $audioSlider.find('.weui-time__start')
        , $end = $audioSlider.find('.weui-time__end')
        , $track = $audioSlider.find('.weui-slider__track')
        , $handler = $audioSlider.find('.weui-slider__handler')
        ;

    if (music) {
        music.duration
            ? init()
            : music.addEventListener('canplaythrough', init);
    }
    music.ontimeupdate = function (e) {
        if (music.currentTime === music.duration) {
            $audioPlayer.removeClass('icon-zanting1').addClass('icon-bofang1')
            $track.css('width', 0);
            $handler.css('left', 0);
            music.currentTime = 0;
            music.pause();
        }
        $start.html(formatSeconds(music.currentTime));
        var rateOfProgress = (music.currentTime / music.duration) * 100;
        $track.css('width', rateOfProgress + '%');
        $handler.css('left', rateOfProgress + '%');
    }

    function init() {
        $end.html(formatSeconds(music.duration));
        $audioSlider.slider(function (percent) {
            music.currentTime = (percent / 100) * music.duration;
            $start.html(formatSeconds(music.currentTime));
        });
        
    }

    $audioPlayer.unbind('tap').on('tap', function (e) {
        e && e.stopPropagation();
        var $this = $(this);
        if ($this.hasClass('icon-bofang1')) {
            $this.removeClass('icon-bofang1').addClass('icon-zanting1');
            music.play();
        } else {
            $this.removeClass('icon-zanting1').addClass('icon-bofang1')
            music.pause();
        };
    })


})

function formatSeconds(value) {
    var secondTime = parseInt(value);// 秒
    var minuteTime = 0;// 分
    var hourTime = 0;// 小时
    if (secondTime > 60) {//如果秒数大于60，将秒数转换成整数
        //获取分钟，除以60取整数，得到整数分钟
        minuteTime = parseInt(secondTime / 60);
        //获取秒数，秒数取佘，得到整数秒数
        secondTime = parseInt(secondTime % 60);
        //如果分钟大于60，将分钟转换成小时
        if (minuteTime > 60) {
            //获取小时，获取分钟除以60，得到整数小时
            hourTime = parseInt(minuteTime / 60);
            //获取小时后取佘的分，获取分钟除以60取佘的分
            minuteTime = parseInt(minuteTime % 60);
        }
    }
    var result = "" + timeString(secondTime);
    result = "" + timeString(minuteTime) + ":" + result;

    if (hourTime > 0) {
        result = "" + timeString(hourTime) + ":" + result;
    }
    return result;
}

function timeString(str) {
    if (str > 10) return str;
    str = '00' + str;
    return str.substr(str.length - 2, 2);

}
